package com.young2000.mybible;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.ActionBar.OnNavigationListener;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SpinnerAdapter;


public class ListenSongActivity extends Activity implements OnNavigationListener {
	private MediaPlayer mp3;
	private ImageView imageView;
	private FileDescriptor fd;
	private boolean is_First;
	RelativeLayout rl;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_listen_song);
		mp3 = null;
		is_First = true;
		imageView = new ImageView(getApplicationContext());
		imageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		imageView.setScaleType(ImageView.ScaleType.FIT_XY);
		//imageView.setImageURI(Uri.parse("file:/"+((MainActivity)MainActivity.mContext).SONG_PATH+"001.gif"));
		rl = (RelativeLayout) findViewById(R.id.Songlayout);
		ChangeSong(((MainActivity)MainActivity.mContext).LatestHymn);
		playPrepareMusicFromSDcard(((MainActivity)MainActivity.mContext).LatestHymn);

		
		// Show the Up button in the action bar.
		setupActionBar();
		ActionBar ab = getActionBar();
		ab.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
		//ab.setBackgroundDrawable(new ColorDrawable(Color.BLUE));
		//SpinnerAdapter sa = ArrayAdapter.createFromResource(this, R.array.list, android.R.layout.simple_spinner_dropdown_item);
		SpinnerAdapter sa = ArrayAdapter.createFromResource(new ContextThemeWrapper(this, android.R.style.Theme_Holo), R.array.list, android.R.layout.simple_spinner_dropdown_item);
		ab.setListNavigationCallbacks(sa, this);
	}
	public boolean onNavigationItemSelected(int itemPosition, long itemId) {
		if (is_First) {
			is_First = false;
		} else {
			ChangeSong(itemPosition+1);
			if (mp3 != null) {
				mp3.release();
				Log.e("mp3Log", "player is released");
			}
			playPrepareMusicFromSDcard(itemPosition+1);
		}

		return true;
	}
	
	private void ChangeSong(Integer num) {
		rl.removeAllViews();
		String temp;
   		if (num<10) {
   			temp = "00" + num.toString();
   		} else if (num<100){
   			temp = "0" + num.toString();       			
   		} else {
   			temp = num.toString();   
   		}
   		
		String path = ((MainActivity)MainActivity.mContext).DB_PATH + temp + ".gif";
		Bitmap image = BitmapFactory.decodeFile(path);
		imageView.setImageBitmap(image);
		rl.addView(imageView);
		((MainActivity)MainActivity.mContext).updateLatestHymn(num);
	}

	public void playPrepareMusicFromSDcard(Integer num) {
		try {
			mp3 = new MediaPlayer();
			//String uri;
			//uri = "file://" + ((MainActivity)MainActivity.mContext).SONG_PATH + "001.MID" ;
			//Uri file=Uri.parse(uri);
			//mp3 = MediaPlayer.create(this, file);

			String temp;
	   		if (num<10) {
	   			temp = "00" + num.toString();
	   		} else if (num<100){
	   			temp = "0" + num.toString();       			
	   		} else {
	   			temp = num.toString();   
	   		}
			File f = new File(((MainActivity)MainActivity.mContext).DB_PATH + "HYMN-" + temp + ".MID");
			FileInputStream fis = new FileInputStream(f);
			fd = fis.getFD();
			mp3.setDataSource( fd );
			mp3.prepare();
			Log.e("mp3Log", "player is prepared");
			//mp3.start();
			//Toast.makeText(getApplicationContext(),
			//		temp + " is in playing", Toast.LENGTH_LONG)
			//	      .show();
		} catch (Exception e) {
			Log.e("Log", "player failed", e);
		}
	}
	
	@Override
	protected void onDestroy () {
		if (mp3 != null) {
			mp3.release();
			mp3 = null;
		}
		super.onDestroy();
	}
	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.listen_song, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		case R.id.action_play:
			if (mp3 != null) {
				if (!mp3.isPlaying())
				{
					mp3.start();
					Log.e("mp3Log", "player is started");
				}
				Log.e("mp3Log", "player is started is done");				
			}
			break;
		case R.id.action_pause:
			if (mp3 != null) {
				if (mp3.isPlaying())
					mp3.pause();
				Log.e("mp3Log", "player is paused");
			}
			break;
		}
		return super.onOptionsItemSelected(item);
	}

}
